 
package ibms_rcp.recharge.parts;

import ibms_rcp.common.ChooseCompanyDialog;
import ibms_rcp.dao.RechargeManager;
import ibms_rcp.model.Company;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.window.Window;

/**
 * 账户充值界面
 * @author diamond-wang
 *
 */
public class AddPart {
	//公司moi_id
	private Text idText;
	// 公司名称
	private Text nameText;
	// 选择公司
	private Button chooseCompanyButton;
	// 联系人
	private Text perText;
	// 电话
	private Text telText;
	//充值金额
	private Text rechargeText;
	//确认充值
	private Button okButton;
	
	@Inject
	public AddPart(Composite parent) {
	}
	
	@PostConstruct
	public void postConstruct(final Composite parent) {
		parent.setLayout(new GridLayout(5, true));
		
		Label title = new Label(parent, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		title.setText("账户充值");
		
		//公司moi id
		idText = new Text(parent, SWT.BORDER);
		idText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		idText.setEditable(false);
		idText.setVisible(false);
		
		new Label(parent, SWT.NONE);
		new Label(parent, SWT.NONE);
		new Label(parent, SWT.NONE);
		
		// 公司名称
		Label nameLabel = new Label(parent, SWT.RIGHT);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		nameLabel.setText("公司名称：");
		nameText = new Text(parent, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));
		nameText.setEditable(false);
		// 选择公司
		chooseCompanyButton = new Button(parent, SWT.NONE);
		chooseCompanyButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				true, false, 1, 1));
		chooseCompanyButton.setText("选择公司");
		//点击弹出选择公司窗口
		chooseCompanyButton.addMouseListener(new MouseAdapter(){
			public void mouseUp(MouseEvent e) {
				ChooseCompanyDialog companyDialog = new ChooseCompanyDialog(parent.getShell());
				companyDialog.create();
				if (companyDialog.open() == Window.OK) {
					Company company = companyDialog.getCompany();
					if(company!=null){
						idText.setText(String.valueOf(company.getId()));
						nameText.setText(company.getName());
						perText.setText(company.getPer());
						telText.setText(company.getTel());
					}
				}
			}
		});
		
		// 联系人
		Label perLabel = new Label(parent, SWT.RIGHT);
		perLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		perLabel.setText("联系人：");
		perText = new Text(parent, SWT.BORDER);
		perText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));
		perText.setEditable(false);
		new Label(parent, SWT.NONE);

		// 电话
		Label telLabel = new Label(parent, SWT.RIGHT);
		telLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		telLabel.setText("电话：");
		telText = new Text(parent, SWT.BORDER);
		telText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));
		telText.setEditable(false);
		new Label(parent, SWT.NONE);
		
		// 充值金额
		Label rechargeLabel1 = new Label(parent, SWT.RIGHT);
		rechargeLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		rechargeLabel1.setText("充值金额：");
		rechargeText = new Text(parent, SWT.BORDER);
		rechargeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));
		Label rechargeLabel2 = new Label(parent, SWT.LEFT);
		rechargeLabel2.setText("元");
		new Label(parent, SWT.NONE);
		
		Label spaceLabel = new Label(parent, SWT.NONE);
		spaceLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 5, 1));
		
		// 确认充值
		okButton = new Button(parent, SWT.NONE);
		okButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				true, false, 5, 1));
		okButton.setText("确认充值");
		okButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				//测试添加记录，为联想添加125元，联想的moi_id 为4839L（为Long类型）
				RechargeManager.saveRecharge(4839L, "联想", 125);
				/*if(RechargeManager.saveRecharge(4839L, "联想", 125)>0){
					
				}else{
					
				}*/
			}
		});
		
	}
	
	
	@PreDestroy
	public void preDestroy() {
		//TODO Your code here
	}
}